package com.team7.member.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.utils.WebUtil;
import com.team7.common.vo.CommonResult;
import com.team7.member.dao.MemberDao;
import com.team7.member.entity.Member;
import com.team7.member.feign.UserFeign;
import com.team7.member.service.MemberService;
import com.team7.member.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: MemberServiceImpl
 * @Description: 会员表的服务实现类
 * @Author: 赵孟阳
 * @Date: 2021/10/19 9:10
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberDao, Member> implements MemberService {
    @Resource
    private MemberDao memberDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private UserFeign userFeign;

    @Override
    public CommonResult getStudentAll(Integer page, Integer limit, MemberVo studentVo) {
        //分页插件
        Page<Member> page1=new Page<>(page,limit);
        //查询条件
        QueryWrapper<Member> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(studentVo.getMobile())){
            wrapper.like("mobile",studentVo.getMobile());
        }
        if (StringUtils.isNotEmpty(studentVo.getNickname())){
            wrapper.like("nickname",studentVo.getNickname());
        }
        if (StringUtils.isNotEmpty(studentVo.getIsDisabled())){
            wrapper.eq("is_disabled",studentVo.getIsDisabled());
        }
        if (studentVo.getGmtCreate().size()!=0){
            wrapper.ge("gmt_create",studentVo.getGmtCreate().get(0));
            wrapper.le("gmt_create",studentVo.getGmtCreate().get(1));
        }
        //查询条件
        Page<Member> memberPage = memberDao.selectPage(page1, wrapper);
        return new CommonResult(2000,"查询成功",memberPage);
    }

    @Override
    public CommonResult modifyStudentInformation(Member member) {
        member.setGmtModified(LocalDateTime.now());
        System.out.println("123");
        int i = memberDao.updateById(member);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult stateChange(Boolean isDisabled,String id) {
        Member member = new Member();
        member.setId(id);
        member.setIsDisabled(isDisabled);
        int i = memberDao.updateById(member);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult findStudentById(String id) {
        Member member = memberDao.selectById(id);
        return new CommonResult(2000,"查询成功",member);
    }

    @Override
    public CommonResult getMemberNum() {
        List<Map> maps = memberDao.selectMember();
        for (Map map : maps) {
            Boolean name = (Boolean) map.get("name");
            if (name){
                map.put("name","普通用户");
            }else {
                map.put("name","讲师");
            }
        }
        return new CommonResult(2000,"查询成功",maps);
    }

    @Override
    public Member findStudentBymobile(String mobile) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        return memberDao.selectOne(wrapper);
    }

    @Override
    public CommonResult tuichuLoginMember() {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("homeToken");
        redisTemplate.delete(token);
        return new CommonResult(2000,"退出成功",null);
    }

    @Override
    @Transactional
    public CommonResult installMember(String mobile, String qianPassword) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Member member1 = memberDao.selectOne(wrapper);
        saveOrUpdate(member1);
        if (member1==null){
            String salt= UUID.randomUUID().toString().replace("-","").substring(1,5);
            Map<String, String> password = userFeign.getPassword(qianPassword, salt);
            Member member = new Member();
            member.setMobile(mobile);
            member.setPassword(password.get("password"));
            member.setSalt(salt);
            int insert = memberDao.insert(member);
            return new CommonResult(2000,"注册成功",null);
        }
        return new CommonResult(5000,"手机号已被注册",null);

    }

    @Override
    public CommonResult updateMemberPassword(String mobile, String newPassword) {
        String salt= UUID.randomUUID().toString().replace("-","").substring(1,5);
        Map<String, String> password = userFeign.getPassword(newPassword, salt);
        int i= memberDao.updatePasswordByMobile(mobile,password.get("password"),salt);
        if (i>0){
            return new CommonResult(2000,"密码修改成功",null);
        }
        return new CommonResult(5000,"密码修改失败",null);
    }

}
